<?php
namespace app\index\controller;
use data\model\Evaluate;
use think\Db;
/**
 * 文章详情页
 * User: Administrator
 * Date: 2017/11/1
 * Time: 15:52
 */
class Article extends Base
{
    /*文章内容*/
    public function index(){
        $id = input('id');
        $art = new \data\model\Article();
        $art->where('artid',$id)->setInc('click');
        /*文章内容*/
        $artres =$art->with(['catas'=>function($query){$query->field('cataid,cataname');}])
            ->where('artid',$id)->find();
//        var_dump($artres->toArray());exit();

//        $pre = Db::name('article')
//            ->alias('a')
//            ->join('cata c','a.cataid = c.cataid','left')
//            ->where('artid','<',$id)
//            ->where('a.cataid',$artres['cataid'])
//            ->limit(1)
//            ->order('artid','desc')
//            ->find();
        /*上一篇*/
        $pre =  $art->pre($id,$artres['cataid']);
//      var_dump($pre->toArray());exit();
        /*下一篇*/
        $next =$art->next($id,$artres['cataid']);
//        $next =Db::name('article')
//            ->alias('a')
//            ->join('cata c','a.cataid = c.cataid','left')
//            ->where('artid','>',$id)
//            ->where('a.cataid',$artres['cataid'])
//            ->limit(1)
//            ->order('artid','asc')
//            ->find();

        /*相关文章*/
        $tag =explode(',',$artres['tag']);
        $tag2 = array();
        foreach ($tag as  $t){
            $tag2[]='%'.$t.'%';
        }
        $relevant =$art->relevant($id,$tag2);
//        var_dump($relevant);exit();
        /*文章评论*/
        $elt = new Evaluate();
        $eva = $elt
            ->where('artid',$id)
            ->order('time','desc')
            ->paginate(10);

        $this->assign(array(
            'art'=>$artres,
            'pre'=>$pre,
            'next'=>$next,
            'relevant' =>$relevant,
            'eva' =>$eva
        ));
        return $this->fetch('index/article/article');
    }

    /*评论文章*/
    public  function evaluate(){
        $data=array(
            'artid'=>input('inpId'),
            'ename'=>input('inpName'),
            'email'=>input('inpEmail'),
            'content'=>input('txaArticle'),
            'url'=>input('inpHomePage'),
        );
        $elt = new Evaluate($data);
        $res = $elt->save();
        if($res){
            $this->redirect('article/index',array('id'=>input('inpId')));
        }else{
            $this->error('index/article/index','发布评论失败');
        }
    }
}